package treenode;

//Tree node that contains two children

public class BinaryTreeNode extends TreeNode  {

    public BinaryTreeNode(String label) {
        super(label);
        left = right = null;
    }

    public BinaryTreeNode(String label, TreeNode left, TreeNode right) {
        super(label);
        this.left = left;
        this.right = right;
    }


    @Override
    public void inFixPrint()  {
        System.out.print("(");
        this.left.inFixPrint();
        System.out.print(label);
        this.right.inFixPrint();
        System.out.print(")");
    }
    
    @Override
    public void postFixPrint() {
        this.left.postFixPrint();
        this.right.postFixPrint();
        System.out.print(label);
    }

    @Override
    public void preFixPrint() {
        System.out.print(label);
        this.left.preFixPrint();
        this.right.preFixPrint();
    }

    private TreeNode left, right;

    @Override
    public int compute() {
        if("+".equals(label)) {
            return this.left.compute() + this.right.compute();
        }
        else if("*".equals(label)) {
            return this.left.compute() * this.right.compute();
        }
        else if("/".equals(label)) {
            return this.left.compute() / this.right.compute();
        }
        else if("-".equals(label)) {
            return this.left.compute() - this.right.compute();
        }
        else if("%".equals(label)) {
            return this.left.compute() % this.right.compute();
        }
        else {
            return 0;
        }
    }
}